{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dc4c1354-bf95-4ad6-9197-64cac3fa1111",
   "metadata": {},
   "source": [
    "# DOM manipulation tests\n",
    "\n",
    "In package `github.com/janpfeifer/gonb/gonbui/dom`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "82c87035-c921-4175-adee-176fb3ea2568",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "%goflags=[\"--cover\" \"--covermode=set\"]\n"
     ]
    }
   ],
   "source": [
    "%goflags --cover --covermode=set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "991075d2-cb0e-4c17-97ec-64174d69b9df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ok\n"
     ]
    }
   ],
   "source": [
    "!if [[ \"${GONB_GIT_ROOT}\" == \"\" ]] ; then \\\n",
    "    echo \"Please set GONB_GIT_ROOT before runnig this notebook!\" 1>&2 ; \\\n",
    "else \\\n",
    "    echo \"ok\" ; \\\n",
    "fi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "79012c7d-ff26-4dc8-b9f5-684e09d49039",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t- Added replace rule for module \"github.com/janpfeifer/gonb\" to local directory \"/home/janpf/Projects/gonb\".\n"
     ]
    }
   ],
   "source": [
    "!*rm -f go.work && go work init && go work use . \"${GONB_GIT_ROOT}\"\n",
    "%goworkfix"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a2ecd95-60f6-4c65-8117-662220a250e1",
   "metadata": {},
   "source": [
    "### Manipulate HTML\n",
    "\n",
    "Manipulate contents of transient output (i.e. an Jupyter output cell being updated), and then persist it so it can be saved and converted (to HTML, text, etc)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0bc8019a-ee78-4e9a-a603-3de6b5915b0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"dom.transient_div_a759e689\"></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "This is a test!<br>\n",
       "And a second test.<br>\n",
       "This is a test!<br>\n",
       "And a second test.<br>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import (\n",
    "    \"github.com/janpfeifer/gonb/gonbui\"\n",
    "    \"github.com/janpfeifer/gonb/gonbui/dom\"\n",
    ")\n",
    "\n",
    "%%\n",
    "rootId := dom.CreateTransientDiv()\n",
    "dom.SetInnerHtml(rootId, \"This is a test!<br>\\n\")\n",
    "dom.Append(rootId, \"And a second test.<br>\\n\")\n",
    "contents := dom.GetInnerHtml(rootId)\n",
    "\n",
    "removeId := \"dom_test_\"+gonbui.UniqueID()\n",
    "dom.Append(rootId, fmt.Sprintf(`<div id=\"%s\">This shouldn't be here</div>`, removeId))\n",
    "dom.Remove(removeId)\n",
    "\n",
    "dom.Append(rootId, contents)  // Duplicate contents.\n",
    "\n",
    "// Repost transient HTML such that it can be saved/converted.\n",
    "dom.Persist(rootId)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d008d868-eb27-4149-8109-0e56621917cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div id=\"dom.transient_div_4117637f\"></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "ok"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%\n",
    "rootId := dom.CreateTransientDiv()\n",
    "dom.Append(rootId, \"error\")\n",
    "dom.SetInnerText(rootId, \"ok\")\n",
    "dom.Persist(rootId)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Go (gonb)",
   "language": "go",
   "name": "gonb"
  },
  "language_info": {
   "codemirror_mode": "",
   "file_extension": ".go",
   "mimetype": "text/x-go",
   "name": "go",
   "nbconvert_exporter": "",
   "pygments_lexer": "",
   "version": "go1.25.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
